package com.ctbri.manage.quality.example;
import com.ctbri.manage.quality.config.YamlReader;
import com.ctbri.manage.quality.single.*;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Test
 *
 */
public class ColStrLenTest
{
    public static void main( String[] args ) {
        //判断字段字符串长度是否满足要求
        //导入yaml配置信息
        final String filePath = "/Users/songyunlong/Desktop/data-manage/data-quality/src/main/resources/colStrLenTestConfig.yaml";
        Map<String, Object> conf = YamlReader.getInstance(filePath).getConf();
        System.out.println(conf);
        final String appName = YamlReader.getInstance(filePath).getString("appName");
        final String masterSet = YamlReader.getInstance(filePath).getString("masterSet");
        final String logLevel = YamlReader.getInstance(filePath).getString("logLevel");
        final String dataExportPath = YamlReader.getInstance(filePath).getString("dataExportPath");
        final String dataSavePath = YamlReader.getInstance(filePath).getString("dataSavePath");
        final String dataSaveStyle = YamlReader.getInstance(filePath).getString("dataSaveStyle");
        final Boolean needStructType = YamlReader.getInstance(filePath).getBoolean("needStructType", false);
        final String needCheckColName = YamlReader.getInstance(filePath).getString("needCheckColName");
        final int len = YamlReader.getInstance(filePath).getInteger("len", false);
        final String dataSource = YamlReader.getInstance(filePath).getString("dataSource");
        final Boolean meetRequirement = YamlReader.getInstance(filePath).getBoolean("meetRequirement", false);
        CheckColumnStringLength checkColumnStringLength = new CheckColumnStringLength(
                appName,
                masterSet,
                logLevel,
                dataExportPath,
                dataSavePath,
                dataSaveStyle,
                len
        );
        StructType structType = null;
        if (needStructType){
            final List<String> colName = YamlReader.getInstance(filePath).getList("colName");
            List<StructField> structFields = new ArrayList<>();
            for (String i: colName) structFields.add(DataTypes.createStructField(i, DataTypes.StringType, true));

            structType = DataTypes.createStructType(structFields);
        }
        try{
            checkColumnStringLength.call(structType, dataSource, needCheckColName, meetRequirement);
        } catch (Exception e){
            e.printStackTrace();
        }

    }
}
